#include "MetaStorageBase.h"
#include <sstream>
#include <comdef.h>
#include "ORACLEADAPTER.h"
#include <comdef.h>
#include <atlbase.h>
#include <msxml.h>
#include <iostream>

MetaStorageBase::MetaStorageBase() 
: m_pDb(NULL)
{
  CoInitialize(NULL);

  m_pDb = DatabaseAdapter::getInstance();
}

MetaStorageBase::~MetaStorageBase() 
{
  if (m_pDb) delete m_pDb;
}

const char* MetaStorageBase::getCurrTime()
{
  SQLServerAdapter * pAdapt = dynamic_cast<SQLServerAdapter*>(m_pDb);
  if (NULL == pAdapt) return NULL;
  return pAdapt->getCurrTime();
}

const char* MetaStorageBase::SelectMsgTime(const char* xml)
{
  char* pszMsgTime = NULL;
  try {
    BSTR vMsgID;
    HRESULT hr;
    VARIANT_BOOL b;

    CComPtr<IXMLDOMDocument> pXMLDOMDoc = NULL; 
    CComPtr<IXMLDOMNode> pXMLDOMNode2 = NULL;
    CComPtr<IXMLDOMNodeList> pXMLDOMNodeList2 = NULL;

    hr = pXMLDOMDoc.CoCreateInstance(__uuidof(DOMDocument));
    hr = pXMLDOMDoc->put_async(false);
    hr = pXMLDOMDoc->put_resolveExternals(false);
    hr = pXMLDOMDoc->loadXML(_bstr_t(xml), &b);

    if (S_OK == hr) hr = pXMLDOMDoc->selectNodes(_bstr_t("//message/header/date"), &pXMLDOMNodeList2);
    if (S_OK == hr) hr = pXMLDOMNodeList2->get_item(0, &pXMLDOMNode2);
    if (S_OK == hr) hr = pXMLDOMNode2->get_text(&vMsgID);

    if (S_OK == hr) {
      pszMsgTime = new char[_bstr_t(vMsgID).length()+1];
      memset(pszMsgTime, 0, _bstr_t(vMsgID).length()+1);
      strncpy(pszMsgTime, _bstr_t(vMsgID), _bstr_t(vMsgID).length());
    }

  } catch (_com_error &e) {
    BSTR b;
    if (e.ErrorInfo()) {
      e.ErrorInfo()->GetDescription(&b);
      char szErr[256];
      sprintf(szErr, "Error: %s\n",_com_util::ConvertBSTRToString(b));
      ::MessageBox(NULL, szErr, "Message Queue Error", MB_ICONERROR | MB_OK);
    }
  } catch (...) { 
  }

  return pszMsgTime;
}


void MetaStorageBase::SelectMsgId(const char* xml, long * lMsgId)
{
  (*lMsgId) = 0;

  try {
    BSTR vMsgID;
    HRESULT hr;
    VARIANT_BOOL b;

    CComPtr<IXMLDOMDocument> pXMLDOMDoc = NULL; 
    CComPtr<IXMLDOMNode> pXMLDOMNode2 = NULL;
    CComPtr<IXMLDOMNodeList> pXMLDOMNodeList2 = NULL;

    hr = pXMLDOMDoc.CoCreateInstance(__uuidof(DOMDocument));
    hr = pXMLDOMDoc->put_async(false);
    hr = pXMLDOMDoc->put_resolveExternals(false);
    hr = pXMLDOMDoc->loadXML(_bstr_t(xml), &b);

    if (S_OK == hr) hr = pXMLDOMDoc->selectNodes(_bstr_t("//message/header/id"), &pXMLDOMNodeList2);
    if (S_OK == hr) hr = pXMLDOMNodeList2->get_item(0, &pXMLDOMNode2);
    if (S_OK == hr) hr = pXMLDOMNode2->get_text(&vMsgID);

    (*lMsgId) = atol(_bstr_t(vMsgID));
  } catch (_com_error &e) {
    BSTR b;
    if (e.ErrorInfo()) {
      e.ErrorInfo()->GetDescription(&b);
      char szErr[256];
      sprintf(szErr, "Error: %s\n",_com_util::ConvertBSTRToString(b));
      ::MessageBox(NULL, szErr, "Message Queue Error", MB_ICONERROR | MB_OK);
    }
  } catch (...) {

  }
}
